package jjn.daily;

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.Scanner;

/**
 * @author Jjn
 * @since 2023/8/28 09:26
 */
public class LeetCode57 {
    public int[][] insert(int[][] intervals, int[] newInterval) {
        Deque<int[]> deque = new ArrayDeque<>();
        int start = 0;
        while (start < intervals.length && intervals[start][1] < newInterval[0]) {
            deque.offer(intervals[start]);
            start++;
        }
        while (start < intervals.length && intervals[start][0] <= newInterval[1]) {
            newInterval[0] = Math.min(intervals[start][0], newInterval[0]);
            newInterval[1] = Math.max(intervals[start][1], newInterval[1]);
            start++;
        }
        deque.offer(newInterval);
        while (start < intervals.length) {
            deque.offer(intervals[start]);
            start++;
        }
        return deque.toArray(new int[deque.size()][]);
    }
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int count = scanner.nextInt();
        int[][] intervals = new int[count][2];
        for (int i = 0; i < count; i++) {
            for (int j = 0; j < 2; j++) {
                intervals[i][j] = scanner.nextInt();
            }
        }
        int[] newInterval = new int[2];
        newInterval[0] = scanner.nextInt();
        newInterval[1] = scanner.nextInt();
        int[][] inserted = new LeetCode57().insert(intervals, newInterval);
        for (int[] insert : inserted) {
            System.out.println(Arrays.toString(insert));
        }
    }
}
